package junit.com.cpi.cornpp.plants;

import java.util.Iterator;
import java.util.List;

import junit.framework.TestCase;

import org.apache.commons.collections.CollectionUtils;

import com.cpi.cornpp.balanceversion.exception.BalanceVersionException;
import com.cpi.cornpp.balanceversion.services.BalanceVersionService;
import com.cpi.cornpp.balanceversion.view.BalanceVersionView;
import com.cpi.cornpp.corntypes.exception.CornTypesException;
import com.cpi.cornpp.corntypes.services.CornTypesService;
import com.cpi.cornpp.instructionsstrategies.exception.InstructionsStrategiesException;
import com.cpi.cornpp.instructionsstrategies.services.InstructionsStrategiesService;
import com.cpi.cornpp.instructionstypes.exception.InstructionsTypesException;
import com.cpi.cornpp.instructionstypes.services.InstructionsTypesService;
import com.cpi.cornpp.plants.dao.PlantDAO;
import com.cpi.cornpp.plants.dto.PlantDTO;
import com.cpi.cornpp.plantversion.exception.PlantVersionException;
import com.cpi.cornpp.plantversion.services.PlantVersionService;
import com.cpi.cornpp.purchase.program.exception.PurchaseProgramException;
import com.cpi.cornpp.purchaseorder.dao.PurchaseOrderVersionDAO;
import com.cpi.cornpp.version.exception.VersionException;
import com.cpi.cornpp.version.services.VersionService;
import com.cpi.cornpp.version.view.MonthPeriodVersionView;

public class PlantDAOTest extends TestCase {

	private PlantDAO plantDAO = null;
	private PlantDTO plantDTO = null;
	
	public PlantDAOTest(String name) {
		super(name);
	}

	protected void setUp() throws Exception {
		super.setUp();
		this.plantDAO = new PlantDAO();
		this.plantDTO = new PlantDTO();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}


	public void testInsert() {
		
		this.plantDTO.setShortName("FEN");
		this.plantDTO.setDescription("Forrajera el Nogal");
		this.plantDTO.setStorageAmount(40000);
		this.plantDTO.setUnloadAmount(50000);
		this.plantDTO.setGrindProtectionDays(10);
				
		try {
			assertTrue(this.plantDAO.insert(plantDTO));
		} catch (Exception e) {
			fail(e.getMessage());
		}
		
	}

	public void testDelete() {
		
		this.plantDTO.setShortName("PSS");		
		this.plantDTO.setPlantId(2);
		
		try {
			assertTrue(this.plantDAO.delete(plantDTO));
		} catch (Exception e) {
			fail(e.getMessage());
		}
		
	}

	public void testUpdate() {
		this.plantDTO.setPlantId(3);
		this.plantDTO.setShortName("PDA");
		this.plantDTO.setDescription("Plantas de Autlan");
		this.plantDTO.setStorageAmount(80000);
		this.plantDTO.setUnloadAmount(40000);
		this.plantDTO.setGrindProtectionDays(5);
		
		try {
			assertTrue(this.plantDAO.update(plantDTO));
		} catch (Exception e) {
			fail(e.getMessage());
		}
		
		
	}

	public void testCheckBeforeInsert() {
		
		this.plantDTO.setShortName("PDA");
		this.plantDTO.setPlantId(3);
		
		try {			
			assertTrue(this.plantDAO.existPlant(this.plantDTO));			
		} catch (Exception e) {
			fail(e.getMessage());
		}	
	}
	
	public void testMonthPeriodVersion(){		
		VersionService service = null;
		MonthPeriodVersionView view = null;
		List list = null;
		Iterator iterator = null;
		try {			
			service = new VersionService();			
			list = service.getVersionsPeriods(3);
			iterator = list.iterator();			
			while(iterator.hasNext()){
				view = (MonthPeriodVersionView) iterator.next();
				System.out.println("ID: " + view.getId());
				System.out.println("DESCRIPTION:  " + view.getDescription());
			}
			assertTrue(list.size() > 0);
			
		} catch (VersionException e) {
			fail(e.getMessage());			
		}
				
	}
	
	public void testPlantVersionService(){
		PlantVersionService service = null;
		List list = null;
		try {
			service = new PlantVersionService();
			list = service.getPlantsVersions(4);
			assertTrue(list.size() > 0);
		} catch (PlantVersionException e) {
			fail(e.getMessage());
		}				
	}
	
	public void testBalanceVersionService(){
		BalanceVersionService service = null;
		BalanceVersionView view = null;
		List list = null;
		Iterator iterator = null;
		try {
			service = new BalanceVersionService();
			list = service.getBalanceVersions(6);			
			iterator = list.iterator();
			while(iterator.hasNext()){
				view = (BalanceVersionView) iterator.next();
				System.out.println("Month Period Version Id: " + view.getMonthPeriodVersionId());
				System.out.println("Balance Version Id: " + view.getBalanceVersionId());
				System.out.println("Amount: " + view.getAmount());
				System.out.println("Out of Credit: " + view.getOutOfCredit());
				System.out.println("Libor: " + view.getLibor());
				System.out.println("Spread: " + view.getSpread());
				System.out.println("Balance: " + view.getBalance());
			}
			assertTrue(list.size() > 0);
			
		} catch (BalanceVersionException e) {
			fail(e.getMessage());
		}
		
	}
	
	
	public void testUpdateBalanceVersionService(){
		
	}
	
	public void testInstructionsServices(){
		InstructionsStrategiesService strategiesService = null;
		InstructionsTypesService typesService = null;
		List straList = null;
		List tList = null;		
		
		try {			
			strategiesService = new InstructionsStrategiesService();
			typesService = new InstructionsTypesService();		
			straList = strategiesService.getInstructionsStrategiesList();
			System.out.println(straList);
			tList = typesService.getInstructionsTypesList();
			System.out.println(tList);
		} catch (InstructionsStrategiesException e) {
			fail(e.getMessage());
		} catch(InstructionsTypesException e1){
			fail(e1.getMessage());
		}								
		
	}
	
	public void testCornTypes(){
		CornTypesService service = null;
		List lista = null;		
		service = new CornTypesService();
		try {			
			lista = service.getListCornTypes();
			assertTrue(CollectionUtils.isEmpty(lista));			
		} catch (CornTypesException e) {
			fail(e.getMessage());
		}
		
	
	}
	
	
	public void testPurchasesList(){
		PurchaseOrderVersionDAO dao = new PurchaseOrderVersionDAO();
		try {
			//dao.getPurchasesOrdersVersionsList(28, new Integer(5519), new Integer(0), new Integer(26), new Integer(35));
			dao.getPurchasesOrdersVersionsList(28, null, null, new Integer(26), new Integer(35));
		} catch (PurchaseProgramException e) {
			fail(e.getMessage());
		}
	}
	
	
	/**
	public void testServiceList(){
		int orderField = 0;
		try {
			this.listViews = service.list(orderField);
			Iterator iterator = this.list.iterator();
			while(iterator.hasNext()){
				PlantDTO e = (PlantDTO)iterator.next();			
				System.out.println("ID " + e.getPlantId());
				System.out.println("SHORT NAME " + e.getShortName());
				System.out.println("DESCRIPTION " + e.getDescription());								
			}
			assertTrue(!this.listViews.isEmpty());
			
		} catch (PlantException e) {
			fail(e.getMessage());
		}
		
	}
	
*/
}
